package com.getpebble.android.main.sections.settings.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.main.sections.settings.activity.LanguageSelectionActivity;
import com.getpebble.android.onboarding.activity.OnboardingActivity;
import com.getpebble.android.util.HttpUtils;
import com.getpebble.android.util.LocaleUtil;
import com.getpebble.android.util.StringUtil;

public class LanguageSelectionStartFragment extends PblBaseFragment {
    private static final String TAG = LanguageSelectionStartFragment.class.getSimpleName();
    private boolean mIsOnboarding = false;
    private final OnClickListener mSetLanguageClickListener = new OnClickListener() {
        public void onClick(View view) {
            Activity activity = LanguageSelectionStartFragment.this.getActivity();
            if (activity == null) {
                Trace.warning(LanguageSelectionStartFragment.TAG, "Activity is null, dropping click event");
                return;
            }
            boolean isInternetConnected = HttpUtils.hasInternetConnection(activity.getApplicationContext());
            if (LanguageSelectionStartFragment.this.mIsOnboarding) {
                OnboardingActivity onboardingActivity = (OnboardingActivity) activity;
                if (isInternetConnected) {
                    onboardingActivity.switchScreenFragment(new LanguagePackManagerFragment());
                } else {
                    onboardingActivity.switchToNoConnectivityFragment();
                }
            } else if (activity instanceof LanguageSelectionActivity) {
                ((LanguageSelectionActivity) activity).switchScreenFragment(new LanguagePackManagerFragment());
            }
        }
    };

    public int getLayoutId() {
        return R.layout.fragment_language_selection_start;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Activity activity = getActivity();
        if (activity == null) {
            Trace.warning(TAG, "init() activity is null, not initializing");
            return;
        }
        this.mIsOnboarding = activity instanceof OnboardingActivity;
        if (!this.mIsOnboarding) {
            container.findViewById(R.id.onboarding_banner).setVisibility(8);
        }
        ((Button) container.findViewById(R.id.set_language_button)).setOnClickListener(this.mSetLanguageClickListener);
        container.findViewById(R.id.language_selection_selected_language_layout).setOnClickListener(this.mSetLanguageClickListener);
        ((TextView) container.findViewById(R.id.language_selection_target_language)).setText(StringUtil.capitalize(LocaleUtil.getLanguageForDevice(PebbleApplication.getConnectedDeviceRecord())));
    }
}
